﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace QLBenhVien
{
    public partial class frmKhamBenh : Form
    {
        public frmKhamBenh()
        {
            InitializeComponent();            
        }

        private void LoadData()
        {
            DataTable dt = DAODanhSachKham.LoadAll(DateTime.Now);
            dgDSBenhNhanKham.DataSource = dt;
            if (dt != null && dt.Rows.Count > 0)
            {
                dgDSBenhNhanKham.Columns["id"].Visible = false;
                dgDSBenhNhanKham.Columns["benhnhan_id"].Visible = false;
                dgDSBenhNhanKham.Columns["ngaykham"].Visible = false;
                dgDSBenhNhanKham.Columns["tennhanvien"].Visible = false;
                dgDSBenhNhanKham.Columns["nhanvien_id"].Visible = false;
                dgDSBenhNhanKham.Columns["benhnhan_id"].Visible = false;
            }
        }

        private void frmKhamBenh_Load(object sender, EventArgs e)
        {
            label1.Text += " " + DateTime.Now.ToShortDateString();
            LoadData();
            AutoCompleteStringCollection cols = new AutoCompleteStringCollection();
            DataTable dt_benh = DAOBenh.LoadAll();
            if (dt_benh.Rows.Count > 0)
            {
                foreach(DataRow dr in dt_benh.Rows)
                {
                    cols.Add(dr["ten"].ToString());
                }
                txtChanDoan.AutoCompleteCustomSource = cols;
                txtChanDoan.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txtChanDoan.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }

            AutoCompleteStringCollection cols_other = new AutoCompleteStringCollection();
            DataTable dt_thuoc = DAOThuoc.LoadAll();
            if (dt_thuoc.Rows.Count > 0)
            {
                foreach (DataRow dr in dt_thuoc.Rows)
                {
                    cols_other.Add(dr["ten"].ToString());
                }
                txtTenThuoc.AutoCompleteCustomSource = cols_other;
                txtTenThuoc.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txtTenThuoc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
        }

        private void dgDSBenhNhanKham_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        DataTable dt_ctdonthuoc = new System.Data.DataTable();
        DataTable dt_donthuoc = new System.Data.DataTable();
        int donthuoc_id = 0;
        private void button6_Click(object sender, EventArgs e)
        {
            if (dgDSBenhNhanKham.SelectedRows.Count > 0)
            {
                txtHoten.Text = dgDSBenhNhanKham.CurrentRow.Cells["TenBenhNhan"].Value.ToString();
                txtID.Text = dgDSBenhNhanKham.CurrentRow.Cells["benhnhan_id"].Value.ToString();
                txtNgaysinh.Text = ((DateTime)dgDSBenhNhanKham.CurrentRow.Cells["ngaysinh"].Value).ToShortDateString();

                DBODonThuoc o = new DBODonThuoc { 
                    benhnhan_id = (int) dgDSBenhNhanKham.CurrentRow.Cells["benhnhan_id"].Value,
                    bacsi_id = frmManHinhChinh.current_user.id
                };
                dt_donthuoc = DAODonThuoc.SearchByBenhNhan(o, DateTime.Now);
                if (dt_donthuoc.Rows.Count > 0)
                {
                    donthuoc_id = int.Parse(dt_donthuoc.Rows[0]["id"].ToString());
                    txtChanDoan.Text = dt_donthuoc.Rows[0]["chandoan"].ToString();
                }
                else
                {
                    donthuoc_id = DAODonThuoc.AddEmpty(o);
                }

                dt_ctdonthuoc = DAOChiTietDonThuoc.SearchByDonThuoc(donthuoc_id);
                dgDonThuoc.DataSource = dt_ctdonthuoc;
                if (dt_ctdonthuoc.Rows.Count > 0)
                {
                    dgDonThuoc.Columns["id"].Visible = false;
                    dgDonThuoc.Columns["donthuoc_id"].Visible = false;
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }
        Bitmap memoryImage;

        private void CaptureScreen()
        {
            Graphics myGraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y + 20, 0, 0, s);
        }

        private void txtLieudung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataRow dr = dt_ctdonthuoc.NewRow();
                dr["donthuoc_id"] = donthuoc_id;
                dr["tenthuoc"] = txtTenThuoc.Text;
                dr["soluong"] = int.Parse(txtSoluong.Text);
                dr["lieudung"] = int.Parse(txtLieudung.Text);
                dt_ctdonthuoc.Rows.Add(dr);
                dgDonThuoc.DataSource = dt_ctdonthuoc;
                dgDonThuoc.Columns["id"].Visible = false;
                dgDonThuoc.Columns["donthuoc_id"].Visible = false;

                txtTenThuoc.ResetText();
                txtTenThuoc.Focus();
            }
        }

        private void txtTenThuoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSoluong.Focus();
            }
        }

        private void txtSoluong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtLieudung.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DBODonThuoc d = new DBODonThuoc { 
                id = donthuoc_id,
                bacsi_id = frmManHinhChinh.current_user.id,
                benhnhan_id = int.Parse(txtID.Text),
                chandoan = txtChanDoan.Text,
                ngaykham = DateTime.Now,
                ngaytaikham = int.Parse(txtNgaytaikham.Text)            
            };
            d.save();
            DataTable tmp = (DataTable)dgDonThuoc.DataSource;
            foreach(DataRow dr in tmp.Rows)
            {
                DBOChiTietDonThuoc c = new DBOChiTietDonThuoc {
                    id = int.Parse(dr["id"].ToString()),
                    donthuoc_id = int.Parse(dr["donthuoc_id"].ToString()),
                    lieudung = int.Parse(dr["lieudung"].ToString()),
                    soluong = int.Parse(dr["soluong"].ToString()),
                    tenthuoc = dr["tenthuoc"].ToString()
                };
                c.save();
            }

            int id = (int)dgDSBenhNhanKham.CurrentRow.Cells["id"].Value;
            DAODanhSachKham.DaThamKham(id);
            LoadData();
            MessageBox.Show("Thông tin đã lưu");
        }
    }
}
